import java.util.HashSet;
import java.util.Set;

public class 最长连续序列2 {
    public int longestConsecutive(int[] nums) {
        int ans = 0;
        Set<Integer> set = new HashSet<>();
        for (int x : nums) {
            set.add(x);
        }
        for (int x : nums) {
            int num1 = x + 1;
            int num2 = x - 1;
            int count = 1;
            while (set.contains(num1)) {
                count++;
                set.remove(num1++);
            }
            while (set.contains(num2)) {
                count++;
                set.remove(num2--);
            }
            ans = Math.max(ans,count);
        }
        return ans;
    }


}
